package com.aoip.telemetry.telemetry.analysis.decode;

import com.aoip.telemetry.telemetry.entity.sn.SnHydrologyEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 水文协议第一轮解析
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/3/4 11:50
 */
@Component
@Slf4j
public class SnHydrologyFrameDecode {

    public static SnHydrologyEntity decode(String originalMessage) {
        int length = originalMessage.length();
        int st = originalMessage.indexOf("ST");
        int stCodeBeginIndex = st + 3;
        int stCodeEndIndex = originalMessage.indexOf(" " , 3);
        String frameStartCharacter = originalMessage.substring(0, 2);
        String stCode = originalMessage.substring(stCodeBeginIndex, stCodeEndIndex);
        String elementData = originalMessage.substring(stCodeEndIndex + 1, length - 4);
        String crc = originalMessage.substring(length - 4, length - 2);
        String messageEndCharacter = originalMessage.substring(length - 2);

        SnHydrologyEntity entity = new SnHydrologyEntity();
        entity.setAllMsg(originalMessage);
        entity.setFrameStartCharacter(frameStartCharacter);
        entity.setStCode(stCode);
        entity.setElementData(elementData);
        entity.setCrc(crc);
        entity.setMessageEndCharacter(messageEndCharacter);
        return entity;
    }
}
